package com.simtop.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.simtop.common.ServerResponse;
import com.simtop.pojo.User;
import com.simtop.service.SwsysteminfoService;
import com.simtop.service.impl.SwsysteminfoServiceImpl;
import com.simtop.util.JwtUtil;
import com.simtop.vo.Swread;
import com.simtop.vo.Swsysteminfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("swsysteminfo")
@CrossOrigin
public class SwsysteminfoController {
    @Autowired
    private SwsysteminfoService service;
    /**
     * 查询/选项查询
     * */
    @PostMapping("selectinfo")
    @ResponseBody
    public ServerResponse selectinfo(@RequestBody String data, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try {
            JSONObject jsonObject = JSONObject.parseObject(data);
            Integer pageNum = jsonObject.getInteger("pageNum");
            Integer pageSize = jsonObject.getInteger("pageSize");
            if(StringUtils.isEmpty(pageNum) || pageNum == 0){
                pageNum = 1;
            }
            if(StringUtils.isEmpty(pageSize) || pageSize == 0){
                pageSize = 10;
            }
            PageHelper.startPage(pageNum,pageSize);
            String types = jsonObject.getString("types");
            Date readTimeBegin = jsonObject.getDate("beginTime");
            Date readTimeEnd = jsonObject.getDate("endTime");
            String order=jsonObject.getString("order");
            String column=jsonObject.getString("column");
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String readTimeBegins = null;
            String readTimeEnds = null;
            if(readTimeBegin != null && readTimeEnd != null) {
                readTimeBegins = df.format(readTimeBegin);
                readTimeEnds = df.format(readTimeEnd);
                List<Swsysteminfo> swreadall = service.selectinfo(readTimeBegins,readTimeEnds,types,column,order);
                PageInfo<Swsysteminfo> pageInfo = new PageInfo<Swsysteminfo>(swreadall);
                return ServerResponse.createBySuccess("获取内容成功",pageInfo);
            }
            List<Swsysteminfo> swreadList = service.selectinfo(readTimeBegins,readTimeEnds,types,column,order);
            PageInfo<Swsysteminfo> pageInfo = new PageInfo<Swsysteminfo>(swreadList);
            return ServerResponse.createBySuccess("获取内容成功",pageInfo);
        }
        catch (Exception e){
            e.printStackTrace();
            return ServerResponse.createByError();
        }
    }
}
